<?php
//print_r(user()->privileges);
class CState
{
	public $states = array();
	public $constants = array();
	function __construct($states)
	{
		foreach($states as &$state)
		{
			$this->states[$state['id']] = $state;
			$this->constants[$state['code']] = $state['id'];
		}
	}
	function id($name)
	{
		return isset($this->constants[$name])?$this->constants[$name]:0;
	}
	function can_view($state, $item = false)
	{
		if(isset($this->states[$state]))
		{
			$roles= $this->states[$state]['allow-view'];
			if(is_string($roles))
			{
				$roles = explode(',',$roles);
			}
			foreach($roles as $role)
			{
				if($role == 'Users' and user()->is_login())
				{
					return true;
				}
				if(isset(user()->privileges[portal()->id][$role]))
				{
					if($item and is_array(user()->privileges[portal()->id][$role]))
					{
						if(!isset(user()->privileges[portal()->id][$role][$item['category_id']]))
						{
							return false;
						}
					}
					return true;
				}
			}
		}		
		return false;
	}
	function can_change($old_state, $new_state, $item = false)
	{
		if(isset($this->states[$old_state]))
		{
			$state = &$this->states[$old_state];
			foreach($state['possibilities'] as $key=>$possibility)
			{
				if(defined($possibility['state']) and constant($possibility['name']) == $new_state)
				{
					if($this->has_privilege($possibility['users'], $item))
					{
						return true;
					}
				}
			}
		}
		return false;
	}
	function has_privilege($roles, $item = false)
	{
		if(is_string($roles))
		{
			$roles = explode(',',$roles);
		}
		foreach($roles as $role)
		{
			if($role == 'Self' and user()->is_login())
			{
				return true;
			}
			if($role == 'Users' and user()->is_login())
			{
				return true;
			}
			if(isset(user()->privileges[portal()->id][$role]))
			{
				if($item and is_array(user()->privileges[portal()->id][$role]))
				{
					if(!isset(user()->privileges[portal()->id][$role][$item['category_id']]))
					{
						return false;
					}
				}
				return true;
			}
		}
		return false;
	}
	
	function code($state)
	{
		print_r($this->states);exit;
		if(isset($this->states[$state]))
		{
			return $this->states[$state]['code'];
		}
	}
	function name($state)
	{
		if(isset($this->states[$state]))
		{
			return $this->states[$state]['name'];
		}
	}
	function get_possibilities($state, $item = false)
	{
		$possibilities = array();
		if(isset($this->states[$state]))
		{
			$state = &$this->states[$state];
			if (isset($state['possibilities'] ))
			{			
				foreach($state['possibilities'] as $key=>$possibility)
				{
					if($this->id($possibility['code']))
					{
						if($this->has_privilege($possibility['users'], $item))
						{
							$possibilities[$this->id($possibility['code'])] = $possibility;
						}
					}
				}
			}
		}
		return $possibilities;
	}
	function get_allow_view_states()
	{
		$states = array();
		foreach($this->states as $state)
		{
			if($this->can_view($state['id']))
			{
				$states[] = $state;
			}
		}
		return $states;
	}
	function get_view_condition()
	{
		$states = $this->get_allow_view_states();
		$condition = false;
		foreach($this->states as $state)
		{
			$roles= $this->states[$state['id']]['allow-view'];
			if(is_string($roles))
			{
				$roles = explode(',',$roles);
			}
			foreach($roles as $role)
			{
				if($role == 'Users' and user()->is_login())
				{
					$condition = EOr($condition, EEqual('status', EString($state['id'])));
					break;
				}
				if(isset(user()->privileges[portal()->id][$role]))
				{
					$sub_condition = EEqual('status',EString($state['id']));
					$category_condition = false;
					if($role != 'Administrators' and is_array(user()->privileges[portal()->id][$role]))
					{
						$categories = user()->privileges[portal()->id][$role];
						foreach($categories as $category_id=>$category)
						{
							$category_condition = EOr($category_condition, EEqual('category_id', EString($category_id)));
						}
						if($category_condition)
						{
							$sub_condition = EAnd($sub_condition, $category_condition);
						}
					}
					$condition = EOr($condition, $sub_condition);
				}
			}
		}
		return $condition;
	}
}
function CState($states)
{
	return new CState($states);
}
?>